# -*- coding: utf-8 -*-
# @Time    : 2020/3/10 13:46
# @Author  : Weymouth
# @Email   : 261229090@qq.com
# @File    : configparser_module.py
# @Software: PyCharm
# ---------------------------------------------------

import configparser

config = configparser.ConfigParser()

config["DEFAULT"] = {
    'ServerAliveInterval': '45',
    'Compression': 'yes',
    'CompressionLevel': '9'
}

config["bitbucket.org"] = {}
config["bitbucket.org"]['User'] = 'hg'

config["topsecret.server.com"] = {}
topsecret = config['topsecret.server.com']
topsecret['Hpst Port'] = '50022'
topsecret['ForwardX11'] = 'no'

config['DEFAULT']['ForwardX11'] = 'yes'


with open('example.ini', 'w') as configfile:
    config.write(configfile)


config.read('example.ini')
print(config.sections())
print(config.defaults())
print(config["bitbucket.org"]['User'])

for key in config["bitbucket.org"]:
    print(key)

#删除键值对
config.remove_section('topsecret.server.com')
config.write(open('example.ini', "w"))    #覆盖原文件
config.remove_option('bitbucket.org', 'user')   #删值
config.write(open('example.ini', "w"))

#修改
config.set('bitbucket.org', 'user', 111111)
config.write(open('example.ini', "w"))